﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace ShadowGame
{
    public class Directions : GameComponent
    {
        private Texture2D directions;

        public Directions(ContentManager content) : base(null)
        {
            directions = content.Load<Texture2D>(@"Images\Directions1");
            animations.Add("upLeft", new Animation(directions, 168, 168, new Rectangle(0, 0, 168, 168), 1, "upLeft"));
            animations["upLeft"].LoopAnimation = true;
            animations["upLeft"].FrameLength = 0.5f;
            PlayAnimation("upLeft");

            animations.Add("up", new Animation(directions, 168, 168, new Rectangle(165, 0, 168, 168), 1, "up"));
            animations["up"].LoopAnimation = true;
            animations["up"].FrameLength = 0.5f;
            PlayAnimation("up");
            
            animations.Add("upRight", new Animation(directions, 168, 168, new Rectangle(330, 0, 168, 168), 1, "upRight"));
            animations["upRight"].LoopAnimation = true;
            animations["upRight"].FrameLength = 0.5f;
            PlayAnimation("upRight");

            animations.Add("right", new Animation(directions, 168, 168, new Rectangle(493, 0, 168, 168), 1, "right"));
            animations["right"].LoopAnimation = true;
            animations["right"].FrameLength = 0.5f;
            PlayAnimation("right");

            animations.Add("left", new Animation(directions, 168, 168, new Rectangle(0, 166, 168, 168), 1, "left"));
            animations["left"].LoopAnimation = true;
            animations["left"].FrameLength = 0.5f;
            PlayAnimation("left");

            animations.Add("downLeft", new Animation(directions, 168, 168, new Rectangle(165, 166, 168, 168), 1, "downLeft"));
            animations["downLeft"].LoopAnimation = true;
            animations["downLeft"].FrameLength = 0.5f;
            PlayAnimation("downLeft");

            animations.Add("down", new Animation(directions, 168, 168, new Rectangle(330, 166, 168, 168), 1, "down"));
            animations["down"].LoopAnimation = true;
            animations["down"].FrameLength = 0.5f;
            PlayAnimation("down");

            animations.Add("downRight", new Animation(directions, 168, 168, new Rectangle(494, 165, 168, 168), 1, "downRight"));
            animations["downRight"].LoopAnimation = true;
            animations["downRight"].FrameLength = 0.5f;

            frameWidth = 168;
            frameHeight = 168;
            drawDepth = 0.825f;
            enabled = false;
            PlayAnimation("downRight");
        }

        public override void Update(GameTime gameTime)
        {
            string newAnimation = "upLeft";
            KeyboardState keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Up) && keyState.IsKeyDown(Keys.Left)) newAnimation = "upLeft";
            else if (keyState.IsKeyDown(Keys.Up) && keyState.IsKeyDown(Keys.Right)) newAnimation = "upRight";
            else if (keyState.IsKeyDown(Keys.Down) && keyState.IsKeyDown(Keys.Left)) newAnimation = "downLeft";
            else if (keyState.IsKeyDown(Keys.Down) && keyState.IsKeyDown(Keys.Right)) newAnimation = "downRight";
            else if (keyState.IsKeyDown(Keys.Right)) newAnimation = "right";
            else if (keyState.IsKeyDown(Keys.Up)) newAnimation = "up";
            else if (keyState.IsKeyDown(Keys.Down)) newAnimation = "down";
            else if (keyState.IsKeyDown(Keys.Left)) newAnimation = "left";

            if (newAnimation != currentAnimation)
                currentAnimation = newAnimation;

            base.Update(gameTime);
        }
    }
}
